package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName RoleContoller
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2020/2/13 16:04
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/role")
public class RoleContoller {

    @Reference// 订阅 dubbo注解
    RoleService roleService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        // 传递当前页，每页显示的记录数，查询条件
        // 响应PageResult，封装总记录数，结果集
        PageResult pageResult = roleService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds){
        try {
            roleService.add(role,permissionIds);
            //新增成功
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        }catch (Exception e){
            //新增失败
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }

    }

    //根据id查询,回显角色数据
    @RequestMapping(value = "/findById")
    public Result findById(Integer id){
        Role role=roleService.findById(id);
        if (role!=null){
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS,role);
        }else {
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    // 使用角色ID，查询角色ID所对应的权限的集合
    @RequestMapping(value = "/findPermissionIdsByRoleId")
    public List<Integer> findPermissionIdsByRoleId(Integer id){
        List<Integer> list = roleService.findPermissionIdsByRoleId(id);
        return list;
    }

    // 修改
    @RequestMapping(value = "/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds){
        try {
            roleService.edit(role,permissionIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }


    // 删除
    @RequestMapping(value = "/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch(RuntimeException ex){
            ex.printStackTrace();
            return new Result(false, ex.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    // 查询所有
    @RequestMapping(value = "/findAll")
    public Result findAll(){
        List<Role> list = roleService.findAll();
        if(list!=null && list.size()>0){
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,list);
        }else{
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


}
